import type { NextAuthConfig } from 'next-auth';

// 将所有不依赖适配器的常见配置放在这里
export const authConfig = {
    secret: process.env.AUTH_SECRET,
    pages: {
        signIn: '/login',
    },
    callbacks: {
        jwt({ token, user }) {
            if (user) {
                token.id = user.id;
            }
            return token;
        },
        session({ session, token }) {
            if (session.user) {
                session.user.id = token.id as string;
            }
            return session;
        },
        authorized({ auth, request: { nextUrl } }) {
            const isLoggedIn = !!auth?.user;

            if (isLoggedIn && nextUrl.pathname.indexOf('/login') > -1) {
                // 登录成功后，登录页跳转到mirrors
                return Response.redirect(new URL('/mirrors', nextUrl));
            }

            return true;
        },
    },
    providers: [],
    trustHost: true,
} satisfies NextAuthConfig;
